#coding=utf-8
import sys,os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')

import random
from datetime import *
from trp.models import *
from django.contrib.auth.models import *
from django.db import connection

area_data = (
    ('北京市', '1156110000'),
    ('天津市', '1156120000'),
    ('河北省', '1156130000'),
    ('石家庄市', '1156130100'),
    ('唐山市', '1156130200'),
    ('秦皇岛市', '1156130300'),
    ('邯郸市', '1156130400'),
    ('邢台市', '1156130500'),
    ('保定市', '1156130600'),
    ('张家口市', '1156130700'),
    ('承德市', '1156130800'),
    ('沧州市', '1156130900'),
    ('廊坊市', '1156131000'),
    ('衡水市', '1156131100'),
    ('山西省', '1156140000'),
    ('太原市', '1156140100'),
    ('大同市', '1156140200'),
    ('阳泉市', '1156140300'),
    ('长治市', '1156140400'),
    ('晋城市', '1156140500'),
    ('朔州市', '1156140600'),
    ('晋中市', '1156140700'),
    ('运城市', '1156140800'),
    ('忻州市', '1156140900'),
    ('临汾市', '1156141000'),
    ('吕梁市', '1156141100'),
    ('内蒙古自治区', '1156150000'),
    ('呼和浩特市', '1156150100'),
    ('包头市', '1156150200'),
    ('乌海市', '1156150300'),
    ('赤峰市', '1156150400'),
    ('通辽市', '1156150500'),
    ('鄂尔多斯市', '1156150600'),
    ('呼伦贝尔市', '1156150700'),
    ('巴彦淖尔市', '1156150800'),
    ('乌兰察布市', '1156150900'),
    ('兴安盟', '1156152200'),
    ('锡林郭勒盟', '1156152500'),
    ('阿拉善盟', '1156152900'),
    ('辽宁省', '1156210000'),
    ('沈阳市', '1156210100'),
    ('大连市', '1156210200'),
    ('鞍山市', '1156210300'),
    ('抚顺市', '1156210400'),
    ('本溪市', '1156210500'),
    ('丹东市', '1156210600'),
    ('锦州市', '1156210700'),
    ('营口市', '1156210800'),
    ('阜新市', '1156210900'),
    ('辽阳市', '1156211000'),
    ('盘锦市', '1156211100'),
    ('铁岭市', '1156211200'),
    ('朝阳市', '1156211300'),
    ('葫芦岛市', '1156211400'),
    ('吉林省', '1156220000'),
    ('长春市', '1156220100'),
    ('吉林市', '1156220200'),
    ('四平市', '1156220300'),
    ('辽源市', '1156220400'),
    ('通化市', '1156220500'),
    ('白山市', '1156220600'),
    ('松原市', '1156220700'),
    ('白城市', '1156220800'),
    ('延边朝鲜族自治州', '1156222400'),
    ('黑龙江省', '1156230000'),
    ('哈尔滨市', '1156230100'),
    ('齐齐哈尔市', '1156230200'),
    ('鸡西市', '1156230300'),
    ('鹤岗市', '1156230400'),
    ('双鸭山市', '1156230500'),
    ('大庆市', '1156230600'),
    ('伊春市', '1156230700'),
    ('佳木斯市', '1156230800'),
    ('七台河市', '1156230900'),
    ('牡丹江市', '1156231000'),
    ('黑河市', '1156231100'),
    ('绥化市', '1156231200'),
    ('大兴安岭地区', '1156232700'),
    ('上海市', '1156310000'),
    ('江苏省', '1156320000'),
    ('南京市', '1156320100'),
    ('无锡市', '1156320200'),
    ('徐州市', '1156320300'),
    ('常州市', '1156320400'),
    ('苏州市', '1156320500'),
    ('南通市', '1156320600'),
    ('连云港市', '1156320700'),
    ('淮安市', '1156320800'),
    ('盐城市', '1156320900'),
    ('扬州市', '1156321000'),
    ('镇江市', '1156321100'),
    ('泰州市', '1156321200'),
    ('宿迁市', '1156321300'),
    ('浙江省', '1156330000'),
    ('杭州市', '1156330100'),
    ('宁波市', '1156330200'),
    ('温州市', '1156330300'),
    ('嘉兴市', '1156330400'),
    ('湖州市', '1156330500'),
    ('绍兴市', '1156330600'),
    ('金华市', '1156330700'),
    ('衢州市', '1156330800'),
    ('舟山市', '1156330900'),
    ('台州市', '1156331000'),
    ('丽水市', '1156331100'),
    ('安徽省', '1156340000'),
    ('合肥市', '1156340100'),
    ('芜湖市', '1156340200'),
    ('蚌埠市', '1156340300'),
    ('淮南市', '1156340400'),
    ('马鞍山市', '1156340500'),
    ('淮北市', '1156340600'),
    ('铜陵市', '1156340700'),
    ('安庆市', '1156340800'),
    ('黄山市', '1156341000'),
    ('滁州市', '1156341100'),
    ('阜阳市', '1156341200'),
    ('宿州市', '1156341300'),
    ('巢湖市', '1156341400'),
    ('六安市', '1156341500'),
    ('亳州市', '1156341600'),
    ('池州市', '1156341700'),
    ('宣城市', '1156341800'),
    ('湖北省', '1156420000'),
    ('武汉市', '1156420100'),
    ('黄石市', '1156420200'),
    ('十堰市', '1156420300'),
    ('宜昌市', '1156420500'),
    ('襄樊市', '1156420600'),
    ('鄂州市', '1156420700'),
    ('荆门市', '1156420800'),
    ('孝感市', '1156420900'),
    ('荆州市', '1156421000'),
    ('黄冈市', '1156421100'),
    ('咸宁市', '1156421200'),
    ('随州市', '1156421300'),
    ('恩施土家族苗族自治州', '1156422800'),
    ('湖南省', '1156430000'),
    ('长沙市', '1156430100'),
    ('株洲市', '1156430200'),
    ('湘潭市', '1156430300'),
    ('衡阳市', '1156430400'),
    ('邵阳市', '1156430500'),
    ('岳阳市', '1156430600'),
    ('常德市', '1156430700'),
    ('张家界市', '1156430800'),
    ('益阳市', '1156430900'),
    ('郴州市', '1156431000'),
    ('永州市', '1156431100'),
    ('怀化市', '1156431200'),
    ('娄底市', '1156431300'),
    ('湘西土家族苗族自治州', '1156433100'),
    ('四川省', '1156510000'),
    ('成都市', '1156510100'),
    ('自贡市', '1156510300'),
    ('攀枝花市', '1156510400'),
    ('泸州市', '1156510500'),
    ('德阳市', '1156510600'),
    ('绵阳市', '1156510700'),
    ('广元市', '1156510800'),
    ('遂宁市', '1156510900'),
    ('内江市', '1156511000'),
    ('乐山市', '1156511100'),
    ('南充市', '1156511300'),
    ('眉山市', '1156511400'),
    ('宜宾市', '1156511500'),
    ('广安市', '1156511600'),
    ('达州市', '1156511700'),
    ('雅安市', '1156511800'),
    ('巴中市', '1156511900'),
    ('资阳市', '1156512000'),
    ('阿坝藏族羌族自治州', '1156513200'),
    ('甘孜藏族自治州', '1156513300'),
    ('凉山彝族自治州', '1156513400'),
    ('贵州省', '1156520000'),
    ('贵阳市', '1156520100'),
    ('六盘水市', '1156520200'),
    ('遵义市', '1156520300'),
    ('安顺市', '1156520400'),
    ('铜仁地区', '1156522200'),
    ('黔西南布依族苗族自治州', '1156522300'),
    ('毕节地区', '1156522400'),
    ('黔东南苗族侗族自治州', '1156522600'),
    ('黔南布依族苗族自治州', '1156522700'),
    ('云南省', '1156530000'),
    ('昆明市', '1156530100'),
    ('曲靖市', '1156530300'),
    ('玉溪市', '1156530400'),
    ('保山市', '1156530500'),
    ('昭通市', '1156530600'),
    ('丽江市', '1156530700'),
    ('普洱市', '1156530800'),
    ('临沧市', '1156530900'),
    ('楚雄彝族自治州', '1156532300'),
    ('红河哈尼族彝族自治州', '1156532500'),
    ('文山壮族苗族自治州', '1156532600'),
    ('西双版纳傣族自治州', '1156532800'),
    ('大理白族自治州', '1156532900'),
    ('德宏傣族景颇族自治州', '1156533100'),
    ('怒江傈僳族自治州', '1156533300'),
    ('迪庆藏族自治州', '1156533400'),
    ('福建省', '1156350000'),
    ('福州市', '1156350100'),
    ('厦门市', '1156350200'),
    ('莆田市', '1156350300'),
    ('三明市', '1156350400'),
    ('泉州市', '1156350500'),
    ('漳州市', '1156350600'),
    ('南平市', '1156350700'),
    ('龙岩市', '1156350800'),
    ('宁德市', '1156350900'),
    ('江西省', '1156360000'),
    ('南昌市', '1156360100'),
    ('景德镇市', '1156360200'),
    ('萍乡市', '1156360300'),
    ('九江市', '1156360400'),
    ('新余市', '1156360500'),
    ('鹰潭市', '1156360600'),
    ('赣州市', '1156360700'),
    ('吉安市', '1156360800'),
    ('宜春市', '1156360900'),
    ('抚州市', '1156361000'),
    ('上饶市', '1156361100'),
    ('山东省', '1156370000'),
    ('济南市', '1156370100'),
    ('青岛市', '1156370200'),
    ('淄博市', '1156370300'),
    ('枣庄市', '1156370400'),
    ('东营市', '1156370500'),
    ('烟台市', '1156370600'),
    ('潍坊市', '1156370700'),
    ('济宁市', '1156370800'),
    ('泰安市', '1156370900'),
    ('威海市', '1156371000'),
    ('日照市', '1156371100'),
    ('莱芜市', '1156371200'),
    ('临沂市', '1156371300'),
    ('德州市', '1156371400'),
    ('聊城市', '1156371500'),
    ('滨州市', '1156371600'),
    ('菏泽市', '1156371700'),
    ('河南省', '1156410000'),
    ('郑州市', '1156410100'),
    ('开封市', '1156410200'),
    ('洛阳市', '1156410300'),
    ('平顶山市', '1156410400'),
    ('安阳市', '1156410500'),
    ('鹤壁市', '1156410600'),
    ('新乡市', '1156410700'),
    ('焦作市', '1156410800'),
    ('濮阳市', '1156410900'),
    ('许昌市', '1156411000'),
    ('漯河市', '1156411100'),
    ('三门峡市', '1156411200'),
    ('南阳市', '1156411300'),
    ('商丘市', '1156411400'),
    ('信阳市', '1156411500'),
    ('周口市', '1156411600'),
    ('驻马店市', '1156411700'),
    ('广东省', '1156440000'),
    ('广州市', '1156440100'),
    ('韶关市', '1156440200'),
    ('深圳市', '1156440300'),
    ('珠海市', '1156440400'),
    ('汕头市', '1156440500'),
    ('佛山市', '1156440600'),
    ('江门市', '1156440700'),
    ('湛江市', '1156440800'),
    ('茂名市', '1156440900'),
    ('肇庆市', '1156441200'),
    ('惠州市', '1156441300'),
    ('梅州市', '1156441400'),
    ('汕尾市', '1156441500'),
    ('河源市', '1156441600'),
    ('阳江市', '1156441700'),
    ('清远市', '1156441800'),
    ('东莞市', '1156441900'),
    ('中山市', '1156442000'),
    ('潮州市', '1156445100'),
    ('揭阳市', '1156445200'),
    ('云浮市', '1156445300'),
    ('广西壮族自治区', '1156450000'),
    ('南宁市', '1156450100'),
    ('柳州市', '1156450200'),
    ('桂林市', '1156450300'),
    ('梧州市', '1156450400'),
    ('北海市', '1156450500'),
    ('防城港市', '1156450600'),
    ('钦州市', '1156450700'),
    ('贵港市', '1156450800'),
    ('玉林市', '1156450900'),
    ('百色市', '1156451000'),
    ('贺州市', '1156451100'),
    ('河池市', '1156451200'),
    ('来宾市', '1156451300'),
    ('崇左市', '1156451400'),
    ('海南省', '1156460000'),
    ('海口市', '1156460100'),
    ('三亚市', '1156460200'),
    ('重庆市', '1156500000'),
    ('西藏自治区', '1156540000'),
    ('拉萨市', '1156540100'),
    ('昌都地区', '1156542100'),
    ('山南地区', '1156542200'),
    ('日喀则地区', '1156542300'),
    ('那曲地区', '1156542400'),
    ('阿里地区', '1156542500'),
    ('林芝地区', '1156542600'),
    ('陕西省', '1156610000'),
    ('西安市', '1156610100'),
    ('铜川市', '1156610200'),
    ('宝鸡市', '1156610300'),
    ('咸阳市', '1156610400'),
    ('渭南市', '1156610500'),
    ('延安市', '1156610600'),
    ('汉中市', '1156610700'),
    ('榆林市', '1156610800'),
    ('安康市', '1156610900'),
    ('商洛市', '1156611000'),
    ('甘肃省', '1156620000'),
    ('兰州市', '1156620100'),
    ('嘉峪关市', '1156620200'),
    ('金昌市', '1156620300'),
    ('白银市', '1156620400'),
    ('天水市', '1156620500'),
    ('武威市', '1156620600'),
    ('张掖市', '1156620700'),
    ('平凉市', '1156620800'),
    ('酒泉市', '1156620900'),
    ('庆阳市', '1156621000'),
    ('定西市', '1156621100'),
    ('陇南市', '1156621200'),
    ('临夏回族自治州', '1156622900'),
    ('甘南藏族自治州', '1156623000'),
    ('青海省', '1156630000'),
    ('西宁市', '1156630100'),
    ('海东地区', '1156632100'),
    ('海北藏族自治州', '1156632200'),
    ('黄南藏族自治州', '1156632300'),
    ('海南藏族自治州', '1156632500'),
    ('果洛藏族自治州', '1156632600'),
    ('玉树藏族自治州', '1156632700'),
    ('海西蒙古族藏族自治州', '1156632800'),
    ('宁夏回族自治区', '1156640000'),
    ('银川市', '1156640100'),
    ('石嘴山市', '1156640200'),
    ('吴忠市', '1156640300'),
    ('固原市', '1156640400'),
    ('中卫市', '1156640500'),
    ('新疆维吾尔自治区', '1156650000'),
    ('乌鲁木齐市', '1156650100'),
    ('克拉玛依市', '1156650200'),
    ('吐鲁番地区', '1156652100'),
    ('哈密地区', '1156652200'),
    ('昌吉回族自治州', '1156652300'),
    ('博尔塔拉蒙古自治州', '1156652700'),
    ('巴音郭楞蒙古自治州', '1156652800'),
    ('阿克苏地区', '1156652900'),
    ('克孜勒苏柯尔克孜自治州', '1156653000'),
    ('喀什地区', '1156653100'),
    ('和田地区', '1156653200'),
    ('伊犁哈萨克自治州', '1156654000'),
    ('塔城地区', '1156654200'),
    ('阿勒泰地区', '1156654300'),
    ('安道尔', '1020000000'),
    ('阿联酋', '1784000000'),
    ('阿富汗', '1004000000'),
    ('安提瓜和巴布达', '1028000000'),
    ('安圭拉', '1660000000'),
    ('阿尔巴尼亚', '1008000000'),
    ('亚美尼亚', '1051000000'),
    ('安哥拉', '1024000000'),
    ('南极洲', '1010000000'),
    ('阿根廷', '1032000000'),
    ('美属萨摩亚', '1016000000'),
    ('奥地利', '1040000000'),
    ('澳大利亚', '1036000000'),
    ('阿鲁巴', '1533000000'),
    ('奥兰群岛', '1248000000'),
    ('阿塞拜疆', '1031000000'),
    ('波黑', '1070000000'),
    ('巴巴多斯', '1052000000'),
    ('孟加拉', '1050000000'),
    ('比利时', '1056000000'),
    ('布基纳法索', '1854000000'),
    ('保加利亚', '1100000000'),
    ('巴林', '1048000000'),
    ('布隆迪', '1108000000'),
    ('贝宁', '1204000000'),
    ('圣巴泰勒米岛', '1652000000'),
    ('百慕大', '1060000000'),
    ('文莱', '1096000000'),
    ('玻利维亚', '1068000000'),
    ('荷兰加勒比区', '1535000000'),
    ('巴西', '1076000000'),
    ('巴哈马', '1044000000'),
    ('不丹', '1064000000'),
    ('布韦岛', '1074000000'),
    ('博茨瓦纳', '1072000000'),
    ('白俄罗斯', '1112000000'),
    ('伯利兹', '1084000000'),
    ('加拿大', '1124000000'),
    ('科科斯群岛', '1166000000'),
    ('刚果（金）', '1180000000'),
    ('中非', '1140000000'),
    ('刚果（布）', '1178000000'),
    ('瑞士', '1756000000'),
    ('科特迪瓦', '1384000000'),
    ('库克群岛', '1184000000'),
    ('智利', '1152000000'),
    ('喀麦隆', '1120000000'),
    ('中国', '1156000000'),
    ('哥伦比亚', '1170000000'),
    ('哥斯达黎加', '1188000000'),
    ('古巴', '1192000000'),
    ('佛得角', '1132000000'),
    ('圣诞岛', '1162000000'),
    ('塞浦路斯', '1196000000'),
    ('捷克', '1203000000'),
    ('德国', '1276000000'),
    ('吉布提', '1262000000'),
    ('丹麦', '1208000000'),
    ('多米尼克', '1212000000'),
    ('多米尼加', '1214000000'),
    ('阿尔及利亚', '1012000000'),
    ('厄瓜多尔', '1218000000'),
    ('爱沙尼亚', '1233000000'),
    ('埃及', '1818000000'),
    ('西撒哈拉', '1732000000'),
    ('厄立特里亚', '1232000000'),
    ('西班牙', '1724000000'),
    ('埃塞俄比亚', '1231000000'),
    ('芬兰', '1246000000'),
    ('斐济群岛', '1242000000'),
    ('马尔维纳斯群岛（福克兰）', '1238000000'),
    ('密克罗尼西亚联邦', '1583000000'),
    ('法罗群岛', '1234000000'),
    ('法国', '1250000000'),
    ('加蓬', '1266000000'),
    ('英国', '1826000000'),
    ('格林纳达', '1308000000'),
    ('格鲁吉亚', '1268000000'),
    ('法属圭亚那', '1254000000'),
    ('根西岛', '1831000000'),
    ('加纳', '1288000000'),
    ('直布罗陀', '1292000000'),
    ('格陵兰', '1304000000'),
    ('冈比亚', '1270000000'),
    ('几内亚', '1324000000'),
    ('瓜德罗普', '1312000000'),
    ('赤道几内亚', '1226000000'),
    ('希腊', '1300000000'),
    ('南乔治亚岛和南桑威奇群岛', '1239000000'),
    ('危地马拉', '1320000000'),
    ('关岛', '1316000000'),
    ('几内亚比绍', '1624000000'),
    ('圭亚那', '1328000000'),
    ('香港', '1344000000'),
    ('赫德岛和麦克唐纳群岛', '1334000000'),
    ('洪都拉斯', '1340000000'),
    ('克罗地亚', '1191000000'),
    ('海地', '1332000000'),
    ('匈牙利', '1348000000'),
    ('印尼', '1360000000'),
    ('爱尔兰', '1372000000'),
    ('以色列', '1376000000'),
    ('马恩岛', '1833000000'),
    ('印度', '1356000000'),
    ('英属印度洋领地', '1086000000'),
    ('伊拉克', '1368000000'),
    ('伊朗', '1364000000'),
    ('冰岛', '1352000000'),
    ('意大利', '1380000000'),
    ('泽西岛', '1832000000'),
    ('牙买加', '1388000000'),
    ('约旦', '1400000000'),
    ('日本', '1392000000'),
    ('肯尼亚', '1404000000'),
    ('吉尔吉斯斯坦', '1417000000'),
    ('柬埔寨', '1116000000'),
    ('基里巴斯', '1296000000'),
    ('科摩罗', '1174000000'),
    ('圣基茨和尼维斯', '1659000000'),
    ('朝鲜', '1408000000'),
    ('韩国', '1410000000'),
    ('科威特', '1414000000'),
    ('开曼群岛', '1136000000'),
    ('哈萨克斯坦', '1398000000'),
    ('老挝', '1418000000'),
    ('黎巴嫩', '1422000000'),
    ('圣卢西亚', '1662000000'),
    ('列支敦士登', '1438000000'),
    ('斯里兰卡', '1144000000'),
    ('利比里亚', '1430000000'),
    ('莱索托', '1426000000'),
    ('立陶宛', '1440000000'),
    ('卢森堡', '1442000000'),
    ('拉脱维亚', '1428000000'),
    ('利比亚', '1434000000'),
    ('摩洛哥', '1504000000'),
    ('摩纳哥', '1492000000'),
    ('摩尔多瓦', '1498000000'),
    ('黑山', '1499000000'),
    ('法属圣马丁', '1663000000'),
    ('马达加斯加', '1450000000'),
    ('马绍尔群岛', '1584000000'),
    ('马其顿', '1807000000'),
    ('马里', '1466000000'),
    ('缅甸', '1104000000'),
    ('蒙古国；蒙古', '1496000000'),
    ('澳门', '1446000000'),
    ('北马里亚纳群岛', '1580000000'),
    ('马提尼克', '1474000000'),
    ('毛里塔尼亚', '1478000000'),
    ('蒙塞拉特岛', '1500000000'),
    ('马耳他', '1470000000'),
    ('毛里求斯', '1480000000'),
    ('马尔代夫', '1462000000'),
    ('马拉维', '1454000000'),
    ('墨西哥', '1484000000'),
    ('马来西亚', '1458000000'),
    ('莫桑比克', '1508000000'),
    ('纳米比亚', '1516000000'),
    ('新喀里多尼亚', '1540000000'),
    ('尼日尔', '1562000000'),
    ('诺福克岛', '1574000000'),
    ('尼日利亚', '1566000000'),
    ('尼加拉瓜', '1558000000'),
    ('荷兰', '1528000000'),
    ('挪威', '1578000000'),
    ('尼泊尔', '1524000000'),
    ('瑙鲁', '1520000000'),
    ('纽埃', '1570000000'),
    ('新西兰', '1554000000'),
    ('阿曼', '1512000000'),
    ('巴拿马', '1591000000'),
    ('秘鲁', '1604000000'),
    ('法属波利尼西亚', '1258000000'),
    ('巴布亚新几内亚', '1598000000'),
    ('菲律宾', '1608000000'),
    ('巴基斯坦', '1586000000'),
    ('波兰', '1616000000'),
    ('圣皮埃尔和密克隆', '1666000000'),
    ('皮特凯恩群岛', '1612000000'),
    ('波多黎各', '1630000000'),
    ('巴勒斯坦', '1275000000'),
    ('葡萄牙', '1620000000'),
    ('帕劳', '1585000000'),
    ('巴拉圭', '1600000000'),
    ('卡塔尔', '1634000000'),
    ('留尼汪', '1638000000'),
    ('罗马尼亚', '1642000000'),
    ('塞尔维亚', '1688000000'),
    ('俄罗斯', '1643000000'),
    ('卢旺达', '1646000000'),
    ('沙特阿拉伯', '1682000000'),
    ('所罗门群岛', '1090000000'),
    ('塞舌尔', '1690000000'),
    ('苏丹', '1729000000'),
    ('瑞典', '1752000000'),
    ('新加坡', '1702000000'),
    ('圣赫勒拿', '1654000000'),
    ('斯洛文尼亚', '1705000000'),
    ('斯瓦尔巴群岛和扬马延岛', '1744000000'),
    ('斯洛伐克', '1703000000'),
    ('塞拉利昂', '1694000000'),
    ('圣马力诺', '1674000000'),
    ('塞内加尔', '1686000000'),
    ('索马里', '1706000000'),
    ('苏里南', '1740000000'),
    ('南苏丹', '1728000000'),
    ('圣多美和普林西比', '1678000000'),
    ('萨尔瓦多', '1222000000'),
    ('叙利亚', '1760000000'),
    ('斯威士兰', '1748000000'),
    ('特克斯和凯科斯群岛', '1796000000'),
    ('乍得', '1148000000'),
    ('法属南部领地', '1260000000'),
    ('多哥', '1768000000'),
    ('泰国', '1764000000'),
    ('塔吉克斯坦', '1762000000'),
    ('托克劳', '1772000000'),
    ('东帝汶', '1626000000'),
    ('土库曼斯坦', '1795000000'),
    ('突尼斯', '1788000000'),
    ('汤加', '1776000000'),
    ('土耳其', '1792000000'),
    ('特立尼达和多巴哥', '1780000000'),
    ('图瓦卢', '1798000000'),
    ('台湾', '1158000000'),
    ('坦桑尼亚', '1834000000'),
    ('乌克兰', '1804000000'),
    ('乌干达', '1800000000'),
    ('美国本土外小岛屿', '1581000000'),
    ('美国', '1840000000'),
    ('乌拉圭', '1858000000'),
    ('乌兹别克斯坦', '1860000000'),
    ('梵蒂冈', '1336000000'),
    ('圣文森特和格林纳丁斯', '1670000000'),
    ('委内瑞拉', '1862000000'),
    ('英属维尔京群岛', '1092000000'),
    ('美属维尔京群岛', '1850000000'),
    ('越南', '1704000000'),
    ('瓦努阿图', '1548000000'),
    ('瓦利斯和富图纳', '1876000000'),
    ('萨摩亚', '1882000000'),
    ('也门', '1887000000'),
    ('马约特', '1175000000'),
    ('南非', '1710000000'),
    ('赞比亚', '1894000000'),
    ('津巴布韦', '1716000000'),
    )

size_data = (
    (300, 100),
    (500, 200),
    (50, 300),
    (200, 100),
    (100, 600),
)


def generate_relations(table, columns, values):
    strs = []
    for value in values:
        tmp = []
        for item in value:
            tmp.append('\'' + str(item) + '\'')
        strs.append('(%s)' % ','.join(tmp))
    sql = 'insert into %s (%s) values %s' % (table, ','.join(columns), ','.join(strs))
    cursor = connection.cursor()
    try:
        cursor.execute(sql)
    finally:
        cursor.close()

def generate_ids(objs):
    for id in range(0, len(objs)):
        objs[id].id = id + 1
    return objs


print 'start'

vendor_num = 100
merchant_num = 1000
media_num = 10
position_num = 10

data = {}
data['platform'] = {}
data['vendor'] = {}
data['merchant'] = {}

accounts = []
users = []
organizations = []
areas = []
material_types = []
material_tags = []
materials = []
media_attrs = []
media = []
position_attrs = []
positions = []
traffics = []
resources = []
allocations = []
strategies = []

def generate_areas():
    print 'generating areas'
    for item in area_data:
        area = Area(name=item[0], code=item[1])
        areas.append(area)
    Area.objects.bulk_create(generate_ids(areas))

def generate_organization(type, order):
    org = Organization()
    if type == 0:
        org.name = 'platform'
    elif type == 1:
        org.name = 'vendor %d' % order
        org.balance = random.uniform(500, 1000)
    else:
        org.name = 'merchant %d' % order
        org.balance = random.uniform(0, 300)
    org.description = ''
    org.contact = ''
    org.type = type
    return org


def generate_organizations():
    print 'generating organizations'
    org = generate_organization(0, 1)
    organizations.append(org)

    for i in range(0, vendor_num):
        org = generate_organization(1, i + 1)
        organizations.append(org)

    for i in range(0, merchant_num):
        org = generate_organization(2, i + 1)
        organizations.append(org)
    Organization.objects.bulk_create(generate_ids(organizations))

import time
def generate_user(type, order):
    user = User()
    if type == 0:
        user.username = 'gomeuser'
        user.last_name = 'gomeuser'
        user.email = 'gomeuser@gome.com.cn'
    elif type == 1:
        user.username = 'vendor%d' % order
        user.last_name = 'vendor%d user' % order
        user.email = 'user@vendor%d.com' % order
    else:
        user.username = 'merchant%d' % order
        user.last_name = 'merchant%d user' % order
        user.email = 'user@merchant%d.com' % order
    user.set_password(user.username)
    return user

def generate_users():
    print 'generating users'
    user = User()
    user.username = 'admin'
    user.last_name = 'admin'
    user.email = 'admin@gome.com.cn'
    user.set_password(user.username)
    users.append(user)

    user = generate_user(0, 1);
    users.append(user)

    for i in range(0, vendor_num):
        user = generate_user(1, i + 1)
        users.append(user)

    for i in range(0, merchant_num):
        user = generate_user(2, i + 1)
        users.append(user)

    User.objects.bulk_create(generate_ids(users))

def generate_account(user):
    account = Account()
    account.user = user
    account.phone = ''
    account.description = ''
    return account

def generate_accounts():
    print 'generating accounts'
    relations = []

    account = Account()
    account.user = users[0]
    account.phone = ''
    account.description = ''
    account.admin = True
    account.id = 1
    accounts.append(account)
    relations.append((account.id, 1))

    for i in range(0, len(organizations)):
        account = generate_account(users[i + 1])
        account.id = i + 2
        accounts.append(account)
        relations.append((account.id, organizations[i].id))
    Account.objects.bulk_create(accounts)
    generate_relations('trp_account_organization', ('account_id', 'organization_id'), relations)

def generate_material_types():
    print 'generating material types'
    material_types.append(MaterialType(name='PNG', id = 1))
    material_types.append(MaterialType(name='JPG', id = 2))
    material_types.append(MaterialType(name='GIF', id = 3))
    material_types.append(MaterialType(name='BMP', id = 4))
    material_types.append(MaterialType(name='SWF', id = 5))
    MaterialType.objects.bulk_create(material_types)

def generate_material_tags():
    print 'generating material tags'
    for i in range(0, 20):
        material_tags.append(MaterialTag(name = 'material tag %d' % (i + 1), id = (i + 1)))
    MaterialTag.objects.bulk_create(material_tags)

def generate_material(org, order):
    material = Material()
    size = random.choice(size_data)
    material.width = size[0]
    material.height = size[1]
    material.size = random.randint(500, 10000)
    r = random.random()
    if r < 0.1:
        material.status = 0
    elif r > 0.9:
        material.status = 2
    else:
        material.status = 1

    material.name = '%s material %d' % (org.name, order)
    material.organization = org
    material.type = random.choice(material_types)
    material.url = 'material.gome.com/material/%d/%d' % (org.id, order)
    material.landingpage = 'www.gome.com/%d' % org.id
    return material


def generate_materials():
    print 'generating materials'
    for org in organizations:
        if org.type == 0:
            num = 100
        elif org.type == 1:
            num = 50
        else:
            num = 30
        for i in range(0, num):
            material = generate_material(org, i + 1)
            materials.append(material)
    Material.objects.bulk_create(generate_ids(materials))
    relations = []
    for material in materials:
        num = random.randint(0, 3)
        tags = random.sample(material_tags, num)
        for tag in tags:
            relations.append((material.id, tag.id))
    generate_relations('trp_material_tags', ('material_id', 'materialtag_id'), relations)

def generate_media_attrs():
    print 'generating media attrs'
    for i in range(0, 20):
        media_attrs.append(MediaAttr(name = 'media attr %d' % (i + 1), id = (i + 1)))
    MediaAttr.objects.bulk_create(media_attrs)

def generate_position_attrs():
    print 'generating position attrs'
    for i in range(0, 20):
        position_attrs.append(PositionAttr(name = 'position attr %d' % (i + 1), id = (i + 1)))
    PositionAttr.objects.bulk_create(position_attrs)

def generate_media():
    print 'generating media'
    for i in range(0, media_num):
        media.append(Media(name = 'media %d' % (i + 1), id = i + 1))
    Media.objects.bulk_create(media)

    relations = []
    for item in media:
        num = random.randint(1, 3)
        attrs = random.sample(media_attrs, num)
        for attr in attrs:
            relations.append((item.id, attr.id))
    generate_relations('trp_media_attrs', ('media_id', 'mediaattr_id'), relations)


def generate_position(media, order):
    position = Position()
    position.name = '%s position %d' % (media.name, order)
    position.media = media
    size = random.choice(size_data)
    position.width = size[0]
    position.height = size[1]
    position.size = random.randint(1, 20) * 1024
    position.min = random.randint(1, 3)
    position.max = random.randint(position.min, 5)
    return position


def generate_positions():
    print 'generating positions'
    for m in media:
        for i in range(0, position_num):
            positions.append(generate_position(m, i + 1))
    Position.objects.bulk_create(generate_ids(positions))
    relations = []
    for item in positions:
        num = random.randint(1, 3)
        attrs = random.sample(position_attrs, num)
        for attr in attrs:
            relations.append((item.id, attr.id))
    generate_relations('trp_position_attrs', ('position_id', 'positionattr_id'), relations)

    relations = []
    for item in positions:
        num = random.randint(2, 4)
        types = random.sample(material_types, num)
        for type in types:
            relations.append((item.id, type.id))
    generate_relations('trp_position_types', ('position_id', 'materialtype_id'), relations)

def generate_traffic(position):
    traffic = Traffic()
    traffic.name = ''
    traffic.start = date(2014, 1, 1)
    traffic.end = date(2014, 12, 31)
    traffic.position = position
    if random.random() < 0.1:
        traffic.active = False
    else:
        traffic.active = True
    traffic.bid_type = random.randint(0, 1)
    if traffic.bid_type == 0:
        traffic.bid_price = 100
    else:
        traffic.bid_price = 0.8
    traffic.estimated_imp = 0
    traffic.estimated_click = 0
    return traffic

def generate_traffics():
    print 'generating traffics'
    for position in positions:
        traffics.append(generate_traffic(position))
    Traffic.objects.bulk_create(generate_ids(traffics))

shared_resources = []

def generate_resource(traffic):
    resource = Resource()
    resource.name = 'default'
    resource.start = date(2014, 1, 1)
    resource.end = date(2014, 12, 31)
    resource.floor = 1.0
    resource.traffic = traffic
    if random.random() < 0.2:
        resource.allow_shared = False
    else:
        shared_resources.append(resource)
    return resource


def generate_resources():
    print 'generating resources'
    for traffic in traffics:
        resources.append(generate_resource(traffic))
    Resource.objects.bulk_create(generate_ids(resources))

def generate_allocation(resource, organization):
    allocation = Allocation()
    allocation.start = date(2014, 1, 1)
    allocation.end = date(2014, 12, 31)
    allocation.resource = resource
    allocation.organization = organization
    allocation.type = random.randint(0, 1)
    if allocation.type == 0:
        allocation.price = 120
    else:
        allocation.price = 1.0
    allocation.amount = 0
    allocation.priority = 10
    return allocation

def generate_allocations():
    print 'generating allocations'
    for resource in resources:
        if not resource.allow_shared:
            allocation = generate_allocation(resource, organizations[0])
            allocations.append(allocation)
    Allocation.objects.bulk_create(generate_ids(allocations))

def generate_strategy(org, start, end, priority, order):
    strategy = Strategy()
    if order == 0:
        strategy.name = 'strategy for %s' % (org.name)
    else:
        strategy.name = 'strategy %d for %s' % (order, org.name)

    strategy.organization = org
    strategy.start = start
    strategy.end = end
    strategy.frequency = 0
    strategy.total_amount = 0
    strategy.daily_amount = 0
    strategy.total_budget = 0
    strategy.daily_budget = 0
    strategy.landingpage = ""
    strategy.consume_type = random.randint(0, 1)
    strategy.priority = priority
    return strategy

def generate_strategies():
    print 'generating strategies'
    relation_areas = []
    relation_hours = []
    relation_resource_prices = []
    relation_material_weights = []

    org_materials = {}

    for material in materials:
        org = material.organization
        if not org_materials.has_key(org.id):
            org_materials[org.id] = []
        org_materials[org.id].append(material)

    hours = []
    for i in range(0, 24):
        hours.append(i)

    id = 1
    for org in organizations:
        if org.type == 0:
            strategy = generate_strategy(org, date(2014, 1, 1), date(2014, 12, 31), -10, 0)
            strategy.id = id
            id += 1
            strategies.append(strategy)
            for resource in resources:
                relation_resource_prices.append((strategy.id, resource.id, 0))
            for material in org_materials[org.id]:
                relation_material_weights.append((strategy.id, material.id, 1, 'group1'))
        else:
            for i in range(0, 20):
                month = random.randint(1, 12)
                start = date(2014, month, 1)
                if month == 12:
                    end = date(2014, 12, 31)
                else:
                    end = date(2014, month + 1, 1) - timedelta(1)
                strategy = generate_strategy(org, start, end, 10, i + 1)
                strategy.id = id
                id += 1
                strategies.append(strategy)

                num = random.randint(3, 5)
                for resource in random.sample(shared_resources, num):
                    relation_resource_prices.append((strategy.id, resource.id, resource.floor + 0.1))
                num = random.randint(5, 10)
                for material in random.sample(org_materials[org.id], num):
                    relation_material_weights.append((strategy.id, material.id, 1, 'group1'))
                if random.random() < 0.5:
                    num = random.randint(5, 15)
                    for area in random.sample(areas, num):
                        relation_areas.append((strategy.id, area.id))
                if random.random() < 0.5:
                    num = random.randint(3, 12)
                    for hour in random.sample(hours, num):
                        relation_hours.append((strategy.id, hour))

    Strategy.objects.bulk_create(strategies)
    generate_relations('trp_resourceprice', ('strategy_id', 'resource_id', 'price'), relation_resource_prices)
    generate_relations('trp_materialweight', ('strategy_id', 'material_id', 'weight', '`group`'), relation_material_weights)
    generate_relations('trp_strategyarea', ('strategy_id', 'area_id'), relation_areas)
    generate_relations('trp_hour', ('strategy_id', 'hour'), relation_hours)

starttime = time.time()
def set_time(time):
    starttime = time
def print_time_cost():
    current = time.time()
    print 'time cost = %f' % (current - starttime)
    set_time(current)


generate_areas()
print_time_cost()
generate_organizations()
print_time_cost()
generate_users()
print_time_cost()
generate_accounts()
print_time_cost()
generate_material_types()
print_time_cost()
generate_material_tags()
print_time_cost()
generate_materials()
print_time_cost()
generate_media_attrs()
print_time_cost()
print_time_cost()
generate_position_attrs()
print_time_cost()
generate_media()
print_time_cost()
print_time_cost()
generate_positions()
print_time_cost()
#generate_traffics()
#print_time_cost()
#generate_resources()
#print_time_cost()
#generate_allocations()
#print_time_cost()
#generate_strategies()
#print_time_cost()


